<?php 
ArrowController::importModel("org.arrowplatform.controls.common.actions.ArrowAbstractUIAction");
class ArrowCreateDialogAction extends ArrowAbstractUIAction{
	
	private $compiledCode;
	
	public function configure(){
		parent::configure();
		$this->addProperties(array(
			"template" => null,
			"width" => false,
			"height" => false,
			"blank" => false,
			"create_data" => "",
			"model" => null
		));
		$this->addRequiredProperties(array("template", "model"));
		//$this->setDisplay(false);
	}
	
	public function init(){
		//$params = $this->getProperty( "create_data" ) ; 
		//$data = array();
		//if( strlen( $params ) > 0 )
		//	$data = explode( "|", $params );
		//if( count($params) > 0 ) {
		//	$data = $params ;
		//} else $data = array() ;
		
			
		parent::init();
		$this->registerJs( get_class($this), dirname(__FILE__)."/resources/ArrowCreateDialogAction.js", $this->generateSelector(), array()/*$data*/ );
		
		$tmpArgs = $this->arguments;
		$tmpArgs["path"] = $this->getProperty("template");
		
		$href = ArrowTemplateLinker::getDefault()->generateTemplateLink( $tmpArgs );

		$ajax = $this->getParent("ArrowCtrlAjax");
		
		$create_data = $this->getProperty( "create_data" ) ;
		
		if( $ajax ){
			$rel = 'rel="'.$this->getProperty("width").'|'.$this->getProperty("height").'|'.$ajax->getId().'|'.$this->getProperty("model").'|'.$create_data.'"';
		}else{
			$rel = 'rel="'.$this->getProperty("width").'|'.$this->getProperty("height").'||'.$this->getProperty("model").'|'.$create_data.'"';
		}
		
		if($this->getProperty("blank"))
			$target='target="_blank"';
		else
			$target='';
		
		$this->compiledCode = '<a '.parent::generateClass().' '.$target.' '.$rel.' href="'.$href.'">'.$this->getName()."</a>";
		
	}
	
	public function generate(  IArrowModel $context = null ){ 
		
	    $tmpArgs = $this->arguments;
    	$tmp = array();    
    	if($context){
			foreach($tmpArgs as &$val){
				if($val{0} == "#")
					$tmp[$val] =  $context[substr($val, 1, -1)];
			}
		}
		
		return strtr( $this->compiledCode, $tmp );
	
	}
}

?>